$.request = (function () { 
	var apiMap = {}; 
	function request(queryStr) { 
		var api = {}; 
		if (apiMap[queryStr]) { return apiMap[queryStr]; } 
		api.queryString = (function () { 
			var urlParams = {}; 
			var e, 
			d = function (s) { return decodeURIComponent(s.replace(/\+/g, " ")); }, 
			q = queryStr.substring(queryStr.indexOf('?') + 1), 
			r = /([^&=]+)=?([^&]*)/g; 
			while (e = r.exec(q)) 	urlParams[d(e[1])] = d(e[2]);
			return urlParams; 
		})(); 
		api.getUrl = function () { 
			var url = queryStr.substring(0, queryStr.indexOf('?') + 1); 
			for (var p in api.queryString) { url += p + '=' + api.queryString[p] + "&";		} 
			if (url.lastIndexOf('&') == url.length - 1) { return url.substring(0, url.lastIndexOf('&')); } 
			return url; 
		} 
		apiMap[queryStr] = api; 
		return api; 
	} 
	$.extend(request, request(window.location.href)); 
	return request; 
})(); 
// -----------------------------------------------------------------------------
// quick rate 
// -----------------------------------------------------------------------------
$(function(){
 $(".btnPositionType input:button").click(function(){
    var myIdarr = $(this).attr('id').split("_"); // index 1 is nickname of button
    if(myIdarr.length != 2){ alert('error -1'); return false; } 
    var oMyDt = $('td.tdRate');//$(this).closest('td.tdRate');
    var twoFace = false;
    var numOfAddVal = $("input.txtAddVal").length; // 1, no xy; 3 get xy
    var addVal01 = $("input.txtAddVal:eq(0)").val();
    if(addVal01 == ''){ addVal01 == '0'; } // return when add value is null
    if(isNaN(addVal01)){ alert('error add Value'); return false; } // return when add value is not numbers
    if(numOfAddVal > 1){
      var addVal02 = $("input.txtAddVal:eq(1)").val();
      if(addVal02 == ''){ addVal02 == '0'; } // return when add value is null
      if(isNaN(addVal02)){ alert('error add Value 02'); return false; } // return when add value is not numbers
      addVal = [addVal01, addVal02];
    }else{
      addVal = [addVal01];
    }   
    var myDtIdArr = oMyDt.attr('id').split("_");
    if(myDtIdArr.length != 2){ alert('error -2'); return false; }
    var myRadio = $(oMyDt).find("input:radio:checked").val();
    var myBtn = myIdarr[1];
    $("input.iptRate").css("background-color", "");    
    var oMyTbl = $(this).closest("table:not(.btnPositionType)").find('tbody.tbdRate:eq(' + myRadio + ')').find("input.iptRate");
    var sNumIdx = 4;
    var sBetIdx = 1;
    var typeChar = myBtn.charAt(0);
    if(myBtn == 'All'){ // all
      $(oMyTbl).addTheVal(addVal);
    }else{
      switch(typeChar){
        case 'A': // four happy
         var typeCharNum = '0' + myBtn.charAt(1);
         var betStr = '6';
         $(oMyTbl).filter(function(idx, ele){
              var myN = $(ele).attr('name').split("_");
              var sNum = myN[sNumIdx];
              var srcNum = myN[sBetIdx]; 
              if(betStr != srcNum){ return false; }
              if(typeCharNum == sNum){ return true;}
              else { return false; }
          }).addTheVal(addVal);
          break;
        case 'B': // red, blue, green
         var typeCharNum = '0' + myBtn.charAt(1);
         var betStr = '5';
         $(oMyTbl).filter(function(idx, ele){
              var myN = $(ele).attr('name').split("_");
              var sNum = myN[sNumIdx];
              var srcNum = myN[sBetIdx]; 
              if(betStr != srcNum){ return false; }
              if(typeCharNum == sNum){ return true;}
              else { return false; }
          }).addTheVal(addVal);
          break;          
          
        case 'C': // double
         var typeCharNum = '0' + myBtn.charAt(1);
         var betStr = ',1,4,7,';
		 if($.request.queryString["gametype"] == '24' && myRadio == 1)
		 {
		     betStr = ',3,';
		 }
         $(oMyTbl).filter(function(idx, ele){
              var myN = $(ele).attr('name').split("_");
              var sNum = myN[sNumIdx];
              var srcNum = ',' + myN[sBetIdx] + ','; 
              if(betStr.lastIndexOf(srcNum) < 0){ return false; }
              if(typeCharNum == sNum){ return true;}
              else { return false; }
          }).addTheVal(addVal);
          break;
          
        case 'D': // big and smail
         var typeCharNum = '0' + myBtn.charAt(1);
         var betStr = ',2,3,8,';
		 if($.request.queryString["gametype"] == '24' && myRadio == 1)
		 {
		     betStr = ',4,';
		 }
         $(oMyTbl).filter(function(idx, ele){
              var myN = $(ele).attr('name').split("_");
              var sNum = myN[sNumIdx];
              var srcNum = ',' + myN[sBetIdx] + ','; 
              if(betStr.lastIndexOf(srcNum) < 0){ return false; }
              if(typeCharNum == sNum){ return true;}
              else { return false; }
          }).addTheVal(addVal);
          break;
        case 'E': // numbers of red, blue, green
         var typeCharNum = myBtn.charAt(1);
         if(typeCharNum == 3){ typeCharNum = 0; }
         $(oMyTbl).filter(function(idx, ele){
              var myN = $(ele).attr('name').split("_");
              var sNum = myN[sNumIdx];
              if(typeCharNum == (sNum % 3)){ return true;}
              else { return false; }
          }).addTheVal(addVal);
          break;
        case 'F': // numbers of four happy
         var typeCharNum = myBtn.charAt(1) - 1;
         $(oMyTbl).filter(function(idx, ele){
              var myN = $(ele).attr('name').split("_");
              var sNum = myN[sNumIdx];
              var subNum = 0;
              if((sNum % 5) == 0){ subNum = 1;}
               
              if(typeCharNum == (Math.floor(sNum / 5) - subNum)){ return true;}
              else { return false; }
          }).addTheVal(addVal);
          break;
        case 'G': // single 1, double 2
        var typeCharNum = myBtn.charAt(1) % 2;
         $(oMyTbl).filter(function(idx, ele){
              var myN = $(ele).attr('name').split("_");
              var sNum = myN[sNumIdx];
              if(sNum == 21){ return false;}
              return (sNum % 2 == typeCharNum);
          }).addTheVal(addVal);
          break;
        case 'H': // big 1, small 2
         var typeCharNum = myBtn.charAt(1) - 1;
         $(oMyTbl).filter(function(idx, ele){
              var myN = $(ele).attr('name').split("_");
              var sNum = myN[sNumIdx];
              if(sNum == 21){ return false;}
              if(typeCharNum){ return (sNum < 11); }
              else{ return (sNum > 10); }
          }).addTheVal(addVal);
          break;
        case 'I': // plus single 1, plus double 2
         var typeCharNum = myBtn.charAt(1) % 2;
         $(oMyTbl).filter(function(idx, ele){
              var myN = $(ele).attr('name').split("_");
              var sNum = myN[sNumIdx];
              if(sNum == 21){ return false;}
              var sNum_1 = sNum.charAt(0) - 0;
              var sNum_2 = sNum.charAt(1) - 0;
              return (( sNum_1 + sNum_2 ) % 2 == typeCharNum);
          }).addTheVal(addVal);
          break;
        case 'J': // number of first postion 
         var typeCharNum = myBtn.charAt(1) - 0;
         $(oMyTbl).filter(function(idx, ele){
              var myN = $(ele).attr('name').split("_");
              var sNum = myN[sNumIdx];
              //if(sNum == 21){ return false;}
              return (sNum.charAt(0) == typeCharNum);
          }).addTheVal(addVal);
          break;
        case 'K': // number of sercond postion
         var typeCharNum = myBtn.charAt(1) - 0;
         $(oMyTbl).filter(function(idx, ele){
              var myN = $(ele).attr('name').split("_");
              var sNum = myN[sNumIdx];
              //if(sNum == 21){ return false;}
              return (sNum.charAt(1) == typeCharNum);
          }).addTheVal(addVal);
          break;
        case 'L': // numbers of four positon
         var typeCharNum = myBtn.charAt(1) - 1;
         $(oMyTbl).filter(function(idx, ele){
              var myN = $(ele).attr('name').split("_");
              var sNum = myN[sNumIdx];
              var subNum = 0;
              if((sNum % 7) == 0){ subNum = 1;}
              if(typeCharNum == (Math.floor(sNum / 7) - subNum)){ return true;}
              else { return false; }
          }).addTheVal(addVal);
          break;
        case 'M': // numbers of chun, fa, pie
         var typeCharNum = myBtn.charAt(1);
         if(typeCharNum == 4){ typeCharNum = 0; }
         $(oMyTbl).filter(function(idx, ele){
              var myN = $(ele).attr('name').split("_");
              var sNum = myN[sNumIdx];
              if(typeCharNum == (sNum % 4)){ return true;}
              else { return false; }
          }).addTheVal(addVal);
          break;
      }
    }
 });
 
 $('.btnmin').add('.btnplus').click(function(){
    var addVal = $("input.txtAddVal").val();
    if(addVal == ''){ addVal == '0'; } // return when add value is null
    if(isNaN(addVal)){ alert('error add Value'); return false; } // return when add value is not numbers
    $("input.iptRate").css("background-color", "");   
    var myRadio = $(this).closest("th").index();
    var oMyTbl = $("tr#myGTr").find('table:eq(' + myRadio + ')').find("input.iptRate");
    if($(this).attr('class') == 'btnmin'){ addVal = addVal * -1 ; }
    $(oMyTbl).addTheVal(addVal);
 });
});
  
$.fn.addTheVal = function(addValArr){
  $(this).each(function(idx, ele){
    var myVal = parseFloat($(ele).val()) - 0;
    var addValStr = $(ele).attr('name');
    var addValIdx = addValStr.charAt(addValStr.length -1 ) - 0;
    if(addValIdx){ addValIdx = addValIdx - 1; }
    else{ addValIdx = addValIdx - 0; }
    addVal = addValArr[addValIdx];
    addVal = addVal - 0.00;
    if(addValIdx > 0 && addVal == 0){return true;}
    myVal = myVal + addVal;
    if(isNaN(addVal)){ return false;}
    if(myVal < 0){ myVal = 0; }
    $(ele).val(myVal);
    if(myVal < 1){
      return $(ele).myRateOKNum().css('background-color', '#FF9291');
    }else{
      return $(ele).myRateOKNum().css('background-color', '#ffff8c');
    }
      
  });
};


$.fn.myRateOKNum = function(){
  var num = $(this).val();
  $(this).val(parseFloat(Math.round(num*1000))/1000);
  return $(this);
};

// -----------------------------------------------------------------------------
// javascript functions
// -----------------------------------------------------------------------------
function xSubNforY(){
  $("input.iptRate").css("background-color", "");
  var addVal = $('#inpXsubNForY').val();
  var oMyTbl = $('.tbdRate').find("input.iptRate");
  var xVal = 0;
  $(oMyTbl).each(function(idx,ele){
      var myN = $(ele).attr('name').split("_");
      var myNX = myN;
      var sXyId = myN[5];
      if(sXyId == 0){ return true; }
      if(sXyId == 1){
        xVal = $(ele).val() - 0;
        return true;
      }
      valY = xVal - addVal;
      if(valY < 1){
        $(ele).val(valY).myRateOKNum().css('background-color', '#FF9291');
      }else{
        $(ele).val(valY).myRateOKNum().css('background-color', '#ffff8c');
      }
      
   });
}
// copy rate subid = 1 to subid = 2~5
function copyRateBtnSub(msg){
  if(confirm(msg)){
    var frmObj = document.forms["frmSrate"];
    $(frmObj).append('<input type="hidden" name="copyRateOK" value="1" />');
    $(frmObj).submit();
  }
  return false;
}


// sub add little btn
function btnAdd1(o, d){
  $(o).addTheVal([d,d]);
}